package com.lp.controller;

import com.lp.userException.CustomException;
import com.lp.po.College;
import com.lp.po.PagingVO;
import com.lp.po.Student;
import com.lp.service.CollegeService;
import com.lp.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/Student")
public class AdminStudentController {
    @Resource
    private StudentService studentService;
    @Resource
    private CollegeService collegeService;

    /***************************用户管理设置(已完成)****************************************/
    //进入首页展现用户
    @RequestMapping("/showStudent")
    public String showStudent(Model model, Integer page) throws Exception {
        List<Student> list=null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        int studentNumber=studentService.getCountStudent();
        //设置总页数
        pagingVO.setTotalCount(this.studentService.getCountStudent());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = studentService.findAll(1);
        } else {
            pagingVO.setToPageNo(page);
            list = studentService.findAll(page);
        }
        model.addAttribute("studentList", list);
        model.addAttribute("pagingVO", pagingVO);
        model.addAttribute("num", studentNumber);
        return "/admin/showStudent";
    }
    @RequestMapping(value="/selectStudentWithName", method={RequestMethod.POST})
    private String selectStudent(String username, Model model, Integer page) throws Exception {
        PagingVO pagingVO = new PagingVO();
        List<Student> list=null;
        int studentNumberByName=0;
        pagingVO.setTotalCount(this.studentService.getCountStudentByName('%'+username+'%'));
        if(username!=null){
            if (page == null || page == 0) {
                pagingVO.setToPageNo(1);
                list = studentService.findByName(1,'%'+username+'%');
                studentNumberByName=studentService.getCountStudentByName('%'+username+'%');
            } else {
                pagingVO.setToPageNo(page);
                list = studentService.findByName(page,'%'+username+'%');
                studentNumberByName=studentService.getCountStudentByName('%'+username+'%');
            }
            model.addAttribute("pagingVO", pagingVO);
            model.addAttribute("studentList", list);
            model.addAttribute("num", studentNumberByName);
            return "/admin/showStudent";
        }else{
            return "/admin/showStudent";
        }
    }
    // 修改用户
    @RequestMapping(value = "/editStudent", method = {RequestMethod.GET})
    public String editStudentUI(Integer userid, Model model) throws Exception {
        if (userid == null) {
            //加入没有带学生id就进来的话就返回学生显示页面
            return "redirect:/admin/Student/showStudent";
        }
        Student student = studentService.findById(userid);
        if (student == null) {
            throw new CustomException("未找到该名学生");
        }
        List<College> list = collegeService.findAll();
        model.addAttribute("collegeList", list);
        model.addAttribute("student", student);
        return "/admin/editStudent";
    }
    // 修改学生信息处理
    @RequestMapping(value = "/doEditStudent", method = {RequestMethod.POST})
    public String editStudent(Student student) throws Exception {
        studentService.updataById(student.getUserid(), student);
        return "redirect:/admin/Student/showStudent";
    }
    // 删除用户
    @RequestMapping(value = "/removeStudent", method = {RequestMethod.GET})
    private String removeStudent(Integer userid) throws Exception {
        if (userid == null) {
            //加入没有带学生id就进来的话就返回学生显示页面
            return "/admin/showStudent";
        }
        studentService.removeById(userid);

        return "redirect:/admin/Student/showStudent";
    }

}
